#include <iostream>
#include <iomanip>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>

using namespace std;

int main()
{
	struct stat buff;
	struct passwd *p_passwd;
	struct group *p_group;
	struct tm *local;
	char p_time[128];
	const char *file;
	time_t t;
//	char *local;

	time(&t);
	file = "myls";
	stat(file,&buff);
	p_passwd = getpwuid(buff.st_uid);
	p_group = getgrgid(buff.st_gid);
	//p_time = ctime(&(buff.st_mtime));
	//local = localtime(&t);
	local = localtime(&(buff.st_mtime));

	if (S_ISDIR(buff.st_mode))
		cout << "d";
	else
		cout << "-";
	if ((buff.st_mode & S_IRUSR))
		cout << "r";
	else
		cout << "-";
	if ((buff.st_mode & S_IWUSR))
		cout << "w";
	else
		cout << "-";
	if ((buff.st_mode & S_IXUSR))
		cout << "x";
	else
		cout << "x";
	if ((buff.st_mode & S_IRGRP))
		cout << "r";
	else
		cout << "-";
	if ((buff.st_mode & S_IWGRP))
		cout << "w";
	else
		cout << "-";
	if ((buff.st_mode & S_IXGRP))
		cout << "x";
	else
		cout << "-";
	if ((buff.st_mode & S_IROTH))
		cout << "r";
	else
		cout << "-";
	if ((buff.st_mode & S_IWOTH))
		cout << "w";
	else
		cout << "-";
	if ((buff.st_mode & S_IXOTH))
		cout << "x";
	else
		cout << "-";
	
	strftime(p_time,128, "%b %e %R", local);

	cout << " ";
	cout << setw(3) << buff.st_nlink << "  ";
	cout  << setw(1)<<p_passwd->pw_name << " " ;
	cout  << setw(2)<<p_group->gr_name << " ";
	cout << setw(10) << buff.st_size << " ";
	cout << p_time;
	cout << " " << file << " ";
	cout << endl;
	return 0;
}
